package com.cloud.code.算法.动态数组;

import java.util.Arrays;

public class Array<E> implements ArrayInterface<E> {

    private int size;

    private Object[] data;

    private static final Object[] DEFAULT_CAPACITY = {};

    private static final int CAPACITY = 10;

    public Array() {
        data = new Object[CAPACITY];
    }

    public Array(int capacity) {
        data = new Object[capacity];
    }

    private void checkIndex(int index) {
        if (index < 0 || index > size)
            throw new IndexOutOfBoundsException();
    }

    @Override
    public boolean add(int index, E e) {
        checkIndex(index);
        checkCapacity();
        data[index] = e;
        size++;
        return true;
    }

    private void checkCapacity() {
        if (size + 1 - data.length > 0) {
            System.out.println("触发扩容，旧的容量：" + data.length);
            int oldCapacity = data.length;
            int newCapacity = oldCapacity + (oldCapacity >> 1);
            data = Arrays.copyOf(data, newCapacity);
            System.out.println("触发扩容，新的容量：" + data.length);
        }
    }

    @Override
    public E get(int index) {
        checkIndex(index);

        return (E) data[index];
    }

    @Override
    public int find(E e) {
        if (e == null)
            for (int i = 0; i < size; i++) {
                if (null == data[i])
                    return i;
            }
        for (int i = 0; i < size; i++) {
            if (e.equals(data[i]))
                return i;
        }
        return -1;
    }

    @Override
    public E remove(int index) {

        checkIndex(index);
        E e = (E) data[index];
        System.arraycopy(data, index + 1, data, index, size - index);
        size--;
        data[size] = null;

        return e;
    }

    @Override
    public boolean remove(E e) {

        int i = find(e);
        if (i != -1) {
            remove(i);
            return true;
        }

        return false;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public void addFast(E e) {
        checkCapacity();
        System.arraycopy(data,0,data,1,size);
        data[0] = e;
        size ++;
    }

    @Override
    public void addLast(E e) {
        checkCapacity();
        data[size] = e;
        size++;
    }

    @Override
    public E removeLast() {
        if(size == 0)
            throw new IndexOutOfBoundsException();
        E e = (E) data[size-1];
        data[size] = null;
        size --;
        return e;
    }

    @Override
    public E removeFast() {
        if(size == 0)
            throw new IndexOutOfBoundsException();
        E e = (E) data[0];
        System.arraycopy(data,1,data,0,size-1);
        data[size] = null;
        size--;
        return e;
    }

    @Override
    public String toString() {

        StringBuffer buffer = new StringBuffer("Array{" +
                "size=" + size +
                '}'+System.getProperty("line.separator"));
        for (int i = 0; i < size; i++) {
            buffer.append(data[i]).append(",");
        }
        return buffer.toString();
    }
}
